package leetcode;

import java.util.Stack;

public class ValidParentheses {
	 
	 public boolean isValid(String s) {
       if(null==s||s.length()%2!=0)return false;
       Stack<Character> now=new Stack<Character>();
       for(int i=0;i<s.length();i++){
           now.push(s.charAt(i));
        	   while(now.size()>1){
        		   Character c=now.pop();
                   if(')'==c&&'('==now.peek()||
                      '}'==c&&'{'==now.peek()||
                      ']'==c&&'['==now.peek()){
                  	 now.pop();
                  	 continue;
        	       }else{
        		     now.push(c);
        		     break;
        		   }
           } 	 
       }
        return now.isEmpty();
	 }
	 
	 public static void main(String[] args) {
		ValidParentheses v=new ValidParentheses();
		System.out.println(v.isValid("(){}[]"));
		System.out.println(v.isValid("({}[])"));
	}
}
